/**
 * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
 * http://github.com/fonoster/fonoster
 *
 * This file is part of Fonoster
 *
 * Licensed under the MIT License (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    https://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";

/**
 * Zod validation schema for the Create/Edit ACL form.
 *
 * This schema defines the expected structure and validation rules for creating or
 * updating an ACL (Access Control List) record. It ensures that:
 * - Each ACL has an optional reference ID.
 * - A human-friendly name is required.
 * - The allow and deny lists are arrays of permission objects (with a required name).
 * - Each permission list can contain a maximum of 10 entries.
 */
export const schema = z.object({
  /**
   * Unique identifier for the ACL.
   *
   * Optional; this is typically generated by the system or provided during updates.
   */
  ref: z.string().nullish(),

  /**
   * Human-friendly name for the ACL.
   *
   * Required; must not be empty.
   */
  name: z.string().nonempty("Friendly Name is required"),

  /**
   * Array of permissions that allow actions.
   *
   * Each permission must have a name and a type (allow).
   * The array can contain up to 10 permissions.
   */
  rules: z
    .array(
      z.object({
        name: z.string().nonempty("Permission name is required"),
        type: z.enum(["allow", "deny"])
      })
    )
    .nonempty("At least one permission is required")
    .max(20, "You can only add up to 20 permissions")
});

/**
 * Type representing the validated data structure returned by the schema.
 *
 * This type is useful for typing the form state, handlers, and submissions.
 */
export type Schema = z.infer<typeof schema>;
